/**
 * 使用promise对axios进行再次封装
 */
import request from './axios.config'
// import QS from 'qs' //如果是form表单需要通过qs模块进行序列化数据

function axios(option) {
  const newOption = option
  if (newOption.params) {
    newOption.params.tempRandom = Math.random() // 解决ie浏览器缓存问题
  }
  return new Promise((resolve, reject) => {
    const requestParams = {
      url: newOption.url,
      method: newOption.method || 'get',
      params: newOption.params || { tempRandom: Math.random() },
      responseType: newOption.responseType || '',
      data: newOption.data || {},
      // data: QS.stringify(option.data) || {} //如果是form表单需要序列化option.data数据
      timeout: newOption.timeout || 15000
    }
    if (newOption.headers) {
      requestParams.headers = newOption.headers
    }
    request(requestParams)
      .then(
        (res) => {
          resolve(res)
        },
        (res) => {
          reject(res)
        }
      )
      .catch((err) => {
        reject(err)
      })
  })
}

export default axios
